﻿using System.Collections.Generic;
using System.Net;
using System.Threading.Tasks;
using JTTD.Common.Attribute;
using JTTD.Common.Enum;
using JTTD.Domain;
using JTTD.Domain.Dtos;
using JTTD.Domain.Dtos.Activity.Request;
using JTTD.Domain.Dtos.Activity.Response;
using JTTD.Domain.IService;
using JTTD.Extensions;
using Microsoft.AspNetCore.Mvc;

namespace JTTD.Controllers
{
    /// <summary>
    /// 商城活动
    /// </summary>
    [Route("api/activitys")]
    [ApiController]
    public class ActivityController : BaseController
    {
        private readonly IActivityService _activityService;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="activityService"></param>
        public ActivityController(IActivityService activityService)
        {
            _activityService = activityService;
        }


        /// <summary>
        /// 获取单个商城活动（后台管理员）
        /// </summary>
        /// <param name="activityId">活动id</param>
        /// <returns></returns>
        [HttpGet]
        [Route("{activityId}")]
        [ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(ApiResult<ActivityInfoResponseDto>))]
        [UserRoot(UserType =UserTypeEnum.Admin)]
        public async Task<IActionResult> GetActivity([FromRoute]int activityId)
        {
            var result = await _activityService.GetActivity(activityId);
            return Ok(result);
        }

        /// <summary>
        /// 分页获取活动列表（后台管理员）
        /// </summary>
        /// <param name="dto">商城活动搜索条件dto</param>
        /// <returns></returns>
        [HttpGet]
        [Route("")]
        [ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(ApiResult<PageResult<ActivityInfoResponseDto>>))]
        [UserRoot(UserType = UserTypeEnum.Admin)]
        public async Task<IActionResult> GetActivityList([FromQuery]SearchActivityRequestDto dto)
        {
            var result = await _activityService.GetActivityPageList(dto);
            return Ok(result);
        }


        /// <summary>
        /// 获取所有进行中的商城活动（微信公众号）
        /// </summary>
        /// <returns></returns>
        [UnAuthorize]
        [HttpGet]
        [Route("enableActivitys")]
        [ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(ApiResult<List<ActivityInfoResponseDto>>))]
        public async Task<IActionResult> GetEnableActivityList()
        {
            var result = await _activityService.GetEnableActivityList();
            return Ok(result);
        }

        /// <summary>
        /// 设置商城活动（后台管理员）
        /// </summary>
        /// <param name="dto">设置活动dto</param>
        /// <returns></returns>
        [HttpPost]
        [Route("activity")]
        [ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(ApiResult<bool>))]
        [UserRoot(UserType = UserTypeEnum.Admin)]
        public async Task<IActionResult> SetActivity([FromBody]SetActivityRequestDto dto)
        {
            var result = await _activityService.SetActivity(dto);
            return Ok(result);
        }

        /// <summary>
        /// 更改启用状态（后台管理员）
        /// </summary>
        /// <param name="activityId">活动id</param>
        /// <param name="isEnable">是否启用</param>
        /// <returns></returns>
        [HttpPut]
        [Route("{activityId}/{isEnable}")]
        [ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(ApiResult<bool>))]
        [UserRoot(UserType = UserTypeEnum.Admin)]
        public async Task<IActionResult> ChangeEnable([FromRoute]int activityId, [FromRoute]bool isEnable)
        {
            var result = await _activityService.ChangeEnable(activityId, isEnable);
            return Ok(result);
        }

        /// <summary>
        /// 删除商城活动（后台管理员）
        /// </summary>
        /// <param name="activityId">活动id</param>
        /// <returns></returns>
        [HttpDelete]
        [Route("{activityId}")]
        [ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(ApiResult<bool>))]
        [UserRoot(UserType = UserTypeEnum.Admin)]
        public async Task<IActionResult> DeleteActivity([FromRoute]int activityId)
        {
            var result = await _activityService.DeleteActivity(activityId);
            return Ok(result);
        }

        /// <summary>
        /// 参与商城积分活动（微信公众号）
        /// </summary>
        /// <param name="activityId">活动id</param>
        /// <returns></returns>
        [HttpGet]
        [Route("JoinActivity")]
        [ProducesResponseType((int)HttpStatusCode.OK,Type=typeof(ApiResult<bool>))]
        [UserRoot(UserType=UserTypeEnum.Ordinary)]
        public async Task<IActionResult> JoinActivity(int activityId)
        {
            var result = await _activityService.JoinActivity(activityId,UserId);
            return Ok(result);
        }



    }
}